using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using GrapeCity.Forguncy.Log;
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using Neo4JConnector.Core;

namespace Neo4JConnector
{

    public abstract class BaseNeo4JOp : Command, ICommandExecutableInServerSideAsync
    {
        [SearchableProperty]
        [FormulaProperty]
        [DisplayName("服务器URL")]
        [OrderWeight(1)]
        public object UrlExpr { get; set; } = "bolt://localhost:7687";

        [SearchableProperty]
        [FormulaProperty]
        [DisplayName("用户名")]
        [OrderWeight(2)]
        public object UserExpr { get; set; } = "neo4j";

        [SearchableProperty]
        [FormulaProperty]
        [DisplayName("密码")]
        [OrderWeight(3)]
        public object PwdExpr { get; set; }

        [SearchableProperty]
        [FormulaProperty]
        [DisplayName("数据库名称")]
        [OrderWeight(4)]
        public object DatabaseExpr { get; set; } = "neo4j";


        internal async Task<KnowledgeGraphClient> getClient(IServerCommandExecuteContext dataContext)
        {
            string url = (await dataContext.EvaluateFormulaAsync(UrlExpr))?.ToString() ?? "";
            string user = (await dataContext.EvaluateFormulaAsync(UserExpr))?.ToString() ?? "";
            string pwd = (await dataContext.EvaluateFormulaAsync(PwdExpr))?.ToString() ?? "";
            string database = (await dataContext.EvaluateFormulaAsync(DatabaseExpr))?.ToString() ?? "";

            if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(user) || string.IsNullOrEmpty(pwd) || string.IsNullOrEmpty(database))
            {
                throw new Exception("请正确配置服务器URL、数据库名、用户名和密码。");
            }

            return new KnowledgeGraphClient(url, database,user, pwd);
        }


        public abstract override string ToString();

        public abstract Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext);

        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }
    }
}
